/********************************************************************************************************
 * @file    app_ui.c
 *
 * @brief   This is the source file for app_ui
 *
 * @author  Zigbee Group
 * @date    2021
 *
 * @par     Copyright (c) 2021, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *			All rights reserved.
 *
 *          Licensed under the Apache License, Version 2.0 (the "License");
 *          you may not use this file except in compliance with the License.
 *          You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 *          Unless required by applicable law or agreed to in writing, software
 *          distributed under the License is distributed on an "AS IS" BASIS,
 *          WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *          See the License for the specific language governing permissions and
 *          limitations under the License.
 *
 *******************************************************************************************************/

#if (__PROJECT_TL_CONTACT_SENSOR__)

/**********************************************************************
 * INCLUDES
 */
#include "tl_common.h"
#include "zb_api.h"
#include "zcl_include.h"
#include "sampleSensor.h"
#include "app_ui.h"

/**********************************************************************
 * LOCAL CONSTANTS
 */

/**********************************************************************
 * TYPEDEFS
 */

/**********************************************************************
 * LOCAL FUNCTIONS
 */
void led_on(u32 pin) {
	drv_gpio_write(pin, LED_ON);
	gpio_setup_up_down_resistor(pin, PM_PIN_PULLUP_10K);
}

void led_off(u32 pin) {
	drv_gpio_write(pin, LED_OFF);
	gpio_setup_up_down_resistor(pin, PM_PIN_PULLDOWN_100K);
}

void light_on(void) {
	led_on(LED1);

}

void light_off(void) {
	led_off(LED1);

}

void light_init(void) {

	led_off(LED1);

}

s32 zclLightTimerCb(void *arg) {
	u32 interval = 0;

	if (g_sensorAppCtx.sta == g_sensorAppCtx.oriSta) {
		g_sensorAppCtx.times--;
		if (g_sensorAppCtx.times <= 0) {
			g_sensorAppCtx.timerLedEvt = NULL;

			return -1;
		}
	}

	g_sensorAppCtx.sta = !g_sensorAppCtx.sta;
	if (g_sensorAppCtx.sta) {
		light_on();
		interval = g_sensorAppCtx.ledOnTime;
	} else {
		light_off();
		interval = g_sensorAppCtx.ledOffTime;
	}

	return interval;
}

void light_blink_start(u8 times, u16 ledOnTime, u16 ledOffTime) {
	u32 interval = 0;
	g_sensorAppCtx.times = times;

	if (!g_sensorAppCtx.timerLedEvt) {
		if (g_sensorAppCtx.oriSta) {
			light_off();
			g_sensorAppCtx.sta = 0;
			interval = ledOffTime;
		} else {
			light_on();
			g_sensorAppCtx.sta = 1;
			interval = ledOnTime;
		}
		g_sensorAppCtx.ledOnTime = ledOnTime;
		g_sensorAppCtx.ledOffTime = ledOffTime;

		g_sensorAppCtx.timerLedEvt
				= TL_ZB_TIMER_SCHEDULE(zclLightTimerCb, NULL, interval);
	}
}

void light_blink_stop(void) {
	if (g_sensorAppCtx.timerLedEvt) {
		TL_ZB_TIMER_CANCEL(&g_sensorAppCtx.timerLedEvt);
		g_sensorAppCtx.times = 0;
		if (g_sensorAppCtx.oriSta) {
			light_on();

		} else {
			light_off();
		}
	}
}

s32 factoryRestart(void *arg) {
	SYSTEM_RESET();
	return -1;
}
/*******************************************************************
 * @brief	Button click detect:
 * 			SW1. keep press button1 5s === factory reset
 * 			SW1. short press button1   === send level step with OnOff command (Up)
 * 			SW2. keep press button2 5s === invoke EZ-Mode
 * 			SW2. short press button2   === send level step with OnOff command (Down)
 *
 */
void buttonKeepPressed(u8 btNum) {
	if (btNum == VK_SW1) {
		printf("leave nwk\n");
		g_sensorAppCtx.state = APP_FACTORY_NEW_DOING;
		zb_factoryReset();
		TL_ZB_TIMER_SCHEDULE(factoryRestart, NULL, 1000);
	}
}

void buttonShortPressed(u8 btNum) {
	if (btNum == VK_SW1) {
		if (zb_isDeviceJoinedNwk()) {
			zb_setPollRate(POLL_RATE);
			printf("SW1 sp, start com\n");
			TL_ZB_TIMER_SCHEDULE(sampleSensor_stopPoll, NULL, STOP_POLL_TIME);
		}
	}
}

void keyScan_keyPressedCB(kb_data_t *kbEvt) {
	//u8 toNormal = 0;
	u8 keyCode = kbEvt->keycode[0];
	//static u8 lastKeyCode = 0xff;

	buttonShortPressed(keyCode);

	if (keyCode == VK_SW1) {
		g_sensorAppCtx.keyPressedTime = clock_time();
		g_sensorAppCtx.state = APP_FACTORY_NEW_SET_CHECK;
	}
}

void keyScan_keyReleasedCB(u8 keyCode) {
	g_sensorAppCtx.state = APP_STATE_NORMAL;
}

s32 app_key_debounce(void *arg) {
	kb_data_t kbData;
	kbData.keycode[0] = VK_SW1;

	if (!drv_pm_wakeupPinValid(g_sensorPmCfg, 1)) {

		if (drv_gpio_read(BUTTON1) == PM_WAKEUP_LEVEL) {
			printf("key pressed\n");
			keyScan_keyPressedCB(&kbData);
		} else {
			printf("key release\n");
			keyScan_keyReleasedCB(kbData.keycode[0]);
		}

		g_sensorPmCfg[0].wakeupLevel = !g_sensorPmCfg[0].wakeupLevel;
		drv_pm_wakeupPinConfig(g_sensorPmCfg, 1);

	}
	return -1;
}

void app_key_handler(void) {

	if (g_sensorAppCtx.state == APP_FACTORY_NEW_SET_CHECK) {
		if (clock_time_exceed(g_sensorAppCtx.keyPressedTime, 5 * 1000 * 1000)) {
			buttonKeepPressed(VK_SW1);
		}
	}
	if(bdb_isIdle() && pm_is_padWakeup()){
		TL_ZB_TIMER_SCHEDULE(app_key_debounce, NULL, 5);
	}

	/*
	 if(kb_scan_key(0 , 1)){
	 if(kb_event.cnt){
	 g_sensorAppCtx.keyPressed = 1;
	 keyScan_keyPressedCB(&kb_event);
	 if(kb_event.cnt == 1){
	 valid_keyCode = kb_event.keycode[0];
	 }
	 }else{
	 keyScan_keyReleasedCB(valid_keyCode);
	 valid_keyCode = 0xff;
	 g_sensorAppCtx.keyPressed = 0;
	 }
	 }*/

}

#endif  /* __PROJECT_TL_CONTACT_SENSOR__ */
